import { EventManager } from "../../engine/EventManager";
import { EventConfig } from "../config/EventConfig";

export class RoomTemplateModel {

    public static clubTemplateList: Array<RoomTemplateModel> = []
    public static ziyouTemplateList:Array<RoomTemplateModel> = []

    public id: number;
    public name: string;
    public gameType: number;
    public cost: number;
    public conf: object
    public clubid: number;
    public creator: number
    public roomType: number;


    public static data2ClubModelList(template: Array<object>) {
        this.clubTemplateList = []
        for (let i = 0; i < template.length; i++) {
            let model: RoomTemplateModel = this.data2Model(template[i])
            this.clubTemplateList[i] = model;
        }
    }

    public static data2ZiyouList(template: Array<object>){
        this.ziyouTemplateList = []
        for (let i = 0; i < template.length; i++) {
            let model: RoomTemplateModel = this.data2Model(template[i])
            this.ziyouTemplateList[i] = model;
        }
    }

    private static data2Model(data: object) {
        let model: RoomTemplateModel = new RoomTemplateModel()
        model.id = data["id"]
        model.name = data["name"]
        model.gameType = data["game_type"]
        model.cost = data["cost"]
        model.conf = data["conf"]
        model.clubid = data["clubid"]
        model.creator = data["creator"]
        model.roomType = data["roomType"]
        return model
    }

    public static getClubListByGameType(gameType: number) {
        let result: Array<RoomTemplateModel> = []
        for (let i = 0; i < this.clubTemplateList.length; i++) {
            let model: RoomTemplateModel = this.clubTemplateList[i]
            if (gameType == 0) {
                result.push(model)
            }
            else if (gameType == model.gameType) {
                result.push(model)
            }
        }
        return result;
    }

    public static getZiyouListByGameType(gameType: number){
        let result: Array<RoomTemplateModel> = []
        for (let i = 0; i < this.ziyouTemplateList.length; i++) {
            let model: RoomTemplateModel = this.ziyouTemplateList[i]
            if (gameType == 0) {
                result.push(model)
            }
            else if (gameType == model.gameType) {
                result.push(model)
            }
        }
        return result;
    }

    public static deleteRoom(id: number) {
        for (let i = this.clubTemplateList.length - 1; i >= 0; i--) {
            let model: RoomTemplateModel = this.clubTemplateList[i]
            if (model.id == id) {
                this.clubTemplateList.splice(i, 1)
            }
        }
        EventManager.instance.dispatch(EventConfig.Event_Update_Club_Room)
    }

}